﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Collections;

namespace PokemonRuby
{
    public class Map
    {
        private int[, ,] _map = null;
        private Hashtable htMetaMap = null;
        private int mapW = 0; // 15
        private string idMap = null;
        public int[, ,] MAP
        {
            get { return _map; }
            set { _map = value; }
        }
        

        public int MapW
        {
            get { return mapW; }
            set { mapW = value; }
        }
        private int mapH = 0; // 15

        public int MapH
        {
            get { return mapH; }
            set { mapH = value; }
        }
        private int mapD = 0; // 2

        public int MapD
        {
            get { return mapD; }
            set { mapD = value; }
        }
        private string vFolderMap = null;
        public Map()
        {
        }
        public Map(string vFolderMap)
        {
            this.vFolderMap = vFolderMap;
        }
        public void LoadMetaMap(int nameMap)
        {
            htMetaMap = new Hashtable();
            string fileName = Path.Combine(GlobalVariable.appPath + "\\metaMap.dat");
            StreamReader sr = File.OpenText(fileName);
            string s = null;
            string[] sar = null;

            s = sr.ReadLine();
            while (s != null)
            {
                sar = s.Split('=');
                htMetaMap.Add(int.Parse(sar[0]), sar[1]);
                s = sr.ReadLine();
            }
            string str = (string)htMetaMap[nameMap];
            sar = str.Split(' ');
            this.mapW = int.Parse(sar[0]);
            this.mapH = int.Parse(sar[1]);
            this.mapD = int.Parse(sar[2]);
        }
        public void LoadMap(int num)
        {
            string mapName = null;
            if (num == 0)
                mapName = "WorldMaps\\" + num;
            else
                mapName = "MiniMaps\\" + num;
            string path = Path.Combine(GlobalVariable.appPath + "\\" +mapName);
            this._map = new int[this.mapW, this.mapH, this.mapD];
            StreamReader sr = File.OpenText(path);
            string s = null;
            string[] sar = null;
            for (int z = 0; z < this.mapD; z++) // map Depth
            {
                for (int y = 0; y < mapH + 1; y++)
                {
                    s = sr.ReadLine();
                    if (s == null)
                        break;
                    sar = s.Split(' ');
                    if (sar[0] == "")
                        break;
                    for (int x = 0; x < sar.Length; x++)
                    {
                        _map[x, y, z] = int.Parse(sar[x]);
                    }
                }
            }
        }

        //public int[, ,] getWorldMap(string appPath, string mapName)
        //{
        //    string path = Path.Combine(appPath + "\\WorldMaps");
        //    LoadMetaMap(appPath, mapName);
        //    this.WorldMap = new int[this.mapW, this.mapH, this.mapD];// ?????
        //    StreamReader sr = File.OpenText(path + "\\" + mapName + ".dat");
        //    string s = null;
        //    string[] sar = null;
        //    for (int z = 0; z < this.mapD; z++) // map Depth
        //    {
        //        for (int y = 0; y < mapH + 1; y++)
        //        {
        //            s = sr.ReadLine();
        //            if (s == null)
        //                break;
        //            sar = s.Split(' ');
        //            if (sar[0] == "")
        //                break;
        //            for (int x = 0; x < sar.Length; x++)
        //            {
        //                WorldMap[x, y, z] = int.Parse(sar[x]);
        //            }
        //        }
        //    }
        //    return WorldMap;
        //}

        //public int[, ,] getMiniMap(string appPath, string mapName)
        //{
        //    string path = Path.Combine(appPath + "\\MiniMaps");
        //    LoadMetaMap(appPath, mapName);
        //    this.MiniMap = new int[this.mapW, this.mapH, this.mapD];// ?????
        //    StreamReader sr = File.OpenText(path + "\\" + mapName + ".dat");
        //    string s = null;
        //    string[] sar = null;
        //    for (int z = 0; z < this.mapD; z++) // map Depth
        //    {
        //        for (int y = 0; y < mapH + 1; y++)
        //        {
        //            s = sr.ReadLine();
        //            if (s == null)
        //                break;
        //            sar = s.Split(' ');
        //            if (sar[0] == "")
        //                break;
        //            for (int x = 0; x < sar.Length; x++)
        //            {
        //                MiniMap[x, y, z] = int.Parse(sar[x]);
        //            }
        //        }
        //    }
        //    return MiniMap;
        //}
    }
}
